//
//  YZPigDocVC.m
//  VET
//
//  Created by YZ on 2018/7/19.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigDocVC.h"
#import "UIView+Frame.h"
#import "YZSiderSelectionView.h"
#import "YZPigDocBaseInfoVC.h"
#import "YZPigDocImmuneRecordVC.h"
#import "YZPigDocHealthVC.h"
#import "YZPigDocCureVC.h"
#import "YZWorkNoteVC.h"

@interface YZPigDocVC ()<UIScrollViewDelegate,YZSiderSelectionViewDelegate>

@property (nonatomic, strong) YZSiderSelectionView *sliderSelectionView;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, strong) YZPigDocBaseInfoVC *infoVC;

@property (nonatomic, strong) YZPigDocImmuneRecordVC *immuneRecordVC;

@property (nonatomic, strong) YZPigDocHealthVC *healthVC;

@property (nonatomic, strong) YZPigDocCureVC *cureVC;

@property (nonatomic, strong) YZWorkNoteVC *noteVC;

@end

@implementation YZPigDocVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    self.currentIndex = 0;
}

- (void)configUI
{
    self.title = @"猪只档案";
    [self.view addSubview:self.sliderSelectionView];
    [self.view addSubview:self.mainScrollView];
    
    //整体UI
    self.infoVC = [YZPigDocBaseInfoVC new];
    self.infoVC.model = self.model;
    self.infoVC.view.frame = CGRectMake(0, kCurrentDeviceNum(20), self.mainScrollView.width, self.mainScrollView.height);
    [self addChildViewController:self.infoVC];
    [self.mainScrollView addSubview:self.infoVC.view];
}

- (void)configUIWithSelectedIndex:(NSInteger)index
{
    switch (index) {
        case 1:
        {
            if (!self.immuneRecordVC) {
                self.immuneRecordVC = [YZPigDocImmuneRecordVC new];
                self.immuneRecordVC.pigId = [NSString stringWithFormat:@"%ld",self.model.pigId];
                self.immuneRecordVC.objectIds = [NSString stringWithFormat:@"猪只%@", self.model.earMark];
                self.immuneRecordVC.view.frame = CGRectMake(kScreenWidth, kCurrentDeviceNum(20), self.mainScrollView.width, self.mainScrollView.height);
                [self addChildViewController:self.immuneRecordVC];
                [self.mainScrollView addSubview:self.immuneRecordVC.view];
            }
        }
            break;
        case 2:
        {
            if (!self.healthVC) {
                self.healthVC = [YZPigDocHealthVC new];
                self.healthVC.model = self.model;
                self.healthVC.pigId = [NSString stringWithFormat:@"%ld",self.model.pigId];
                self.healthVC.view.frame = CGRectMake(2*kScreenWidth, kCurrentDeviceNum(20), self.mainScrollView.width, self.mainScrollView.height);
                [self addChildViewController:self.healthVC];
                [self.mainScrollView addSubview:self.healthVC.view];
            }
        }
            break;
        case 3:
        {
            if (!self.cureVC) {
                self.cureVC = [YZPigDocCureVC new];
                self.cureVC.pigId = [NSString stringWithFormat:@"%ld",self.model.pigId];
                self.cureVC.treatmentObjectList = [NSString stringWithFormat:@"猪只%@", self.model.earMark];
                self.cureVC.view.frame = CGRectMake(3*kScreenWidth, kCurrentDeviceNum(20), self.mainScrollView.width, self.mainScrollView.height);
                [self addChildViewController:self.cureVC];
                [self.mainScrollView addSubview:self.cureVC.view];
            }
        }
            break;
        case 4:
        {
            if (!self.noteVC) {
                self.noteVC = [YZWorkNoteVC new];
                self.noteVC.type = @"swine";
                self.noteVC.objId = self.model.pigId;
                self.noteVC.view.frame = CGRectMake(4*kScreenWidth, kCurrentDeviceNum(20), self.mainScrollView.width, self.mainScrollView.height);
                [self addChildViewController:self.noteVC];
                [self.mainScrollView addSubview:self.noteVC.view];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -- YZSiderSelectionViewDelegate

- (void)itemDidSelectedWithIndex:(NSInteger)index
{
    [self configUIWithSelectedIndex:index];
    self.mainScrollView.contentOffset = CGPointMake(index*kScreenWidth, 0);
}

#pragma mark -- UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/kScreenWidth;
    [self configUIWithSelectedIndex:index];
    self.sliderSelectionView.currentIndex = index;
}

- (YZSiderSelectionView *)sliderSelectionView
{
    if (!_sliderSelectionView) {
        _sliderSelectionView = [[YZSiderSelectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kCurrentDeviceNum(50)) showPopViewWithMoreButton:NO];
        _sliderSelectionView.backgroundColor = kColorWithHex(0xFFFFFF);
        _sliderSelectionView.selectedColor = kColorWithHex(0x434343);
        _sliderSelectionView.LineColor = kColorWithHex(0x32BE60);
        _sliderSelectionView.currentIndex= 0;
        _sliderSelectionView.noSlectedColor = kColorWithHex(0x7A7A7A);
        _sliderSelectionView.myTitleArray = @[@"基本信息",@"免疫记录",@"保健记录",@"治疗记录",@"笔记信息"];
        _sliderSelectionView.delegate = self;
    }
    return _sliderSelectionView;
}

- (UIScrollView *)mainScrollView
{
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.sliderSelectionView.bottom, kScreenWidth, kScreenHeight - kTopViewHeight - YZ_STATUSBAR_HEIGHT - YZ_BOTTOM_MARGIN - self.sliderSelectionView.frame.size.height )];
        _mainScrollView.backgroundColor = kColorWithHex(0xE9EAEB);
        _mainScrollView.delegate = self;
        _mainScrollView.contentOffset = CGPointZero;
        _mainScrollView.contentSize = CGSizeMake(kScreenWidth*5, _mainScrollView.frame.size.height);
        _mainScrollView.pagingEnabled = YES;
    }
    return _mainScrollView;
}
@end
